package com.huanghuai.retail.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.huanghuai.retail.common.CustomException;
import com.huanghuai.retail.common.R;
import com.huanghuai.retail.entity.PInformation;
import com.huanghuai.retail.entity.SInventory;
import com.huanghuai.retail.entity.SWarnInfo;
import com.huanghuai.retail.entity.Storage;
import com.huanghuai.retail.service.ISWarnInfoService;
import com.huanghuai.retail.service.InformationService;
import com.huanghuai.retail.service.StorageService;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Controller
@RestController
@RequestMapping("/warn")
public class WarnController
{
    @Resource
    ISWarnInfoService warnInfoService;

    @Resource
    StorageService storageService;

    @Resource
    InformationService informationService;

    /*
    * 对预警信息的分页查询  默认五页一页十个
    * 默认查询未处理的  点击传入了isHandled查询已经处理过的
    * */
    @GetMapping("/page")
    public R<Page> selectWarn(@RequestParam(value = "page",defaultValue = "1") int page,
                              @RequestParam(value = "pageSize",defaultValue = "10") int pageSize,
                              @RequestParam(value = "isHandled",defaultValue = "10") int isHandled,
                              @RequestParam(value = "storageName",required = false) String storageName,
                              @RequestParam(value = "productName",required = false) String productName,
                              @RequestParam(value = "storageIds",required = false) Long[] storageIds
                              )
    {
        Page<SWarnInfo> pageInfo = new Page<>(page,pageSize);
        LambdaQueryWrapper<SWarnInfo> queryWrapper= new LambdaQueryWrapper<>();
        queryWrapper.eq(isHandled==1,SWarnInfo::getIsHandle,isHandled);
        queryWrapper.eq(isHandled==0,SWarnInfo::getIsHandle,isHandled);
        if (storageIds!=null&&storageIds.length>0)
        {
            for (Long storageId : storageIds) {
                queryWrapper.eq(storageId!=null,SWarnInfo::getStorageId,storageId);
            }
        }
//        @RequestParam(value = "startTime",required = false)@DateTimeFormat(pattern = "yyyy-MM-dd") LocalDateTime startTime,
//        @RequestParam(value = "endTime",required = false)@DateTimeFormat(pattern = "yyyy-MM-dd") LocalDateTime endTime
//
//        if (startTime!=null&&endTime!=null){
//            queryWrapper.ge(SWarnInfo::getDate,startTime);
//            queryWrapper.gt(SWarnInfo::getDate,endTime);
//        }

        if (storageName!=null)
        {
            LambdaQueryWrapper<Storage> queryWrapper1 =new LambdaQueryWrapper<>();
            queryWrapper1.like(Storage::getStorageName,storageName);
            List<Storage> list = storageService.list(queryWrapper1);
            if (list==null) throw  new CustomException("仓库未查到数据");
            for (Storage storage : list)
            {
                Long id = storage.getId();
                queryWrapper.eq(SWarnInfo::getStorageId,id);
            }
        }

        if (productName!=null)
        {
            LambdaQueryWrapper<PInformation> queryWrapper2 = new LambdaQueryWrapper<>();
            queryWrapper2.like(PInformation::getName,productName);
            List<PInformation> list = informationService.list(queryWrapper2);
            if (list==null) throw  new CustomException("仓库未查到数据");

                for (PInformation pInformation : list) {
                    queryWrapper.eq(SWarnInfo::getProductId,pInformation.getId());
                }
        }
        queryWrapper.orderByDesc(SWarnInfo::getDate);
        warnInfoService.page(pageInfo, queryWrapper);
        List<SWarnInfo> list = warnInfoService.list(queryWrapper);
//        List<SWarnInfo> list = pageInfo.getRecords();
        List<SWarnInfo> warnings = list.stream().map((item) -> {
            LambdaQueryWrapper<Storage> queryWrapper1 = new LambdaQueryWrapper<>();
            queryWrapper1.eq(Storage::getId, item.getStorageId());
            Storage storage = storageService.getOne(queryWrapper1);
            if (storage != null) {
                item.setStorageName(storage.getStorageName());
            }
//             根据 产品的id 去查找他的名字便于显示
            LambdaQueryWrapper<PInformation> queryWrapper2 = new LambdaQueryWrapper<>();
//            QueryWrapper<PInformation> queryWrapper2 = new QueryWrapper<>();
            queryWrapper2.eq(PInformation::getId,item.getProductId());
            PInformation information = informationService.getOne(queryWrapper2);
            if (information != null) {
                item.setProductName(information.getName());
            }
            return item;
        }).collect(Collectors.toList());

        pageInfo.setRecords(warnings);
        return R.success(pageInfo);
    }

    /**
     * 传过来多个id封装在数组中
     *
     */
    @DeleteMapping("/delete")
    public R<Object> deleteWarn(@RequestBody  List<Long> ids)
    {
//        if (ids.length<=0)
//        {
//            return R.error("未传入id");
//        }
//        List<Long> longs = Arrays.asList(ids);
        for (Long id : ids) {
            SWarnInfo byId = warnInfoService.getById(id);
            if ( "0".equals(byId.getIsHandle())) {
               return R.error(byId.getProductName()+"还未处理不能删除");
            }
        }
        boolean b = warnInfoService.removeByIds(ids);
        if (!b)
        {
            return R.error("删除失败");
        }

        return R.success("删除成功");
    }



}
